package com.atyjh.reggie.common;

import com.atyjh.reggie.utils.UserHolder;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 填充处理器
 *
 * @author yjh
 * @since jdk1.8.0_311 2022.06.16
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
//        metaObject.setValue("createTime", LocalDateTime.now());
//        metaObject.setValue("updateTime", LocalDateTime.now());
//        metaObject.setValue("createUser", UserHolder.getUser().getId());
//        metaObject.setValue("updateUser", UserHolder.getUser().getId());
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "createUser", Long.class, UserHolder.getUser().getId());
        this.strictUpdateFill(metaObject, "updateUser", Long.class, UserHolder.getUser().getId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
//        metaObject.setValue("updateTime", LocalDateTime.now());
//        metaObject.setValue("updateUser", UserHolder.getUser().getId());
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        this.strictUpdateFill(metaObject, "updateUser", Long.class, UserHolder.getUser().getId());

    }
}
